package com.learn.xueer.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseActivity1
import com.learn.xueer.app.ext.init
import com.learn.xueer.databinding.ActivitySectionDetailBinding
import com.learn.xueer.ui.adapter.SectionDetailAdapter
import com.learn.xueer.ui.viewmodel.RequestHearViewModel
import kotlinx.android.synthetic.main.activity_section_detail.recyclerView
import me.hgj.jetpackmvvm.ext.parseState

class SectionDetailActivity : BaseActivity1<RequestHearViewModel, ActivitySectionDetailBinding>() {

    private lateinit var id: String
    private lateinit var pid: String
    private val sectionDetailAdapter: SectionDetailAdapter by lazy {
        SectionDetailAdapter(
            arrayListOf()
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(mViewModel)
        mViewBind.viewmodel = mViewModel
        mViewBind.click = ProxyClick()
        id = intent.getStringExtra("id")!!
        mViewModel.sectionTitle.set(intent.getStringExtra("title"))
        pid = intent.getIntExtra("pid", 0).toString()

        recyclerView.init(LinearLayoutManager(this), sectionDetailAdapter)
        sectionDetailAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, HearSayTestActivity::class.java)
            intent.putExtra("id", sectionDetailAdapter.data[position].versionId)
            intent.putExtra("title", sectionDetailAdapter.data[position].moduleName)
            startActivity(intent)
        }

        mViewModel.sectionDetailReq(id, pid)
    }

    override fun createObserver() {
        mViewModel.sectionDetailResult.observe(this, Observer { resultState ->
            parseState(resultState, {
                mViewModel.sectionDetailTitle.set(it?.name)
                sectionDetailAdapter.setList(it?.hsr)
            }, {})
        })
    }

    inner class ProxyClick {
        fun returnBack() {
            finish()
        }
    }
}